package mt.client;

import java.awt.Label;
import java.util.Date;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.table.AbstractTableModel;
import javax.xml.crypto.Data;

import mt.Order;

/**
 * 
 * @author victorandrade
 */
public class OrderTableModel extends AbstractTableModel {

	private final int NICKNAME = 0;
	private final int STOCK = 1;
	private final int BUY = 2;
	private final int NUMBER_OF_UNIT = 3;
	private final int PRINCE_PER_UNIT = 4;
	private final int DATA = 5;

	private final String column[] = { "Nickname:", "Stock", "Buy", "Num/unit",
			"Prince per unit" };
	private List<Order> listOrder;

	/**
	 * Contruct
	 * 
	 * @param listOrder
	 */
	public OrderTableModel(List<Order> listOrder) {
		this.listOrder = listOrder;
	}

	/**
	 * @return o valor total de linhas na tabela
	 */
	@Override
	public int getRowCount() {
		return listOrder.size();
	}

	/**
	 * @return o valor total de colunas na tabela
	 */
	@Override
	public int getColumnCount() {
		return column.length;
	}

	/**
	 * @param um
	 *            numero int da coluna
	 * @return o nome da coluna
	 */
	@Override
	public String getColumnName(int columnIndex) {
		return column[columnIndex];
	}

	/**
	 * @return o tipo de dados para cada coluna.
	 * 
	 */
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		try {
			return this.getValueAt(0, columnIndex).getClass();
		} catch (Exception e) {
			throw new IndexOutOfBoundsException("Coluna invalida");
		}

	}

	/**
	 * @param conforme
	 *            a coluna e linha
	 */
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Order oneOrder = listOrder.get(rowIndex);

		switch (columnIndex) {
		case NICKNAME:
			return oneOrder.getNickname();
		case STOCK:
			return oneOrder.getStock();
		case BUY:
			return oneOrder.isBuyOrder();
		case NUMBER_OF_UNIT:
			return oneOrder.getNumberOfUnits();
		case PRINCE_PER_UNIT:
			return oneOrder.getPricePerUnit();
		default:
			throw new IndexOutOfBoundsException("Coluna invalida");
		}
	}

	/**
	 * 
	 * @param rowIndex
	 * @return o valor da linha indicada
	 */
	public Order getValue(int rowIndex) {
		return listOrder.get(rowIndex);
	}

	/**
	 * 
	 * @param ordem
	 * @return o indice da ordem
	 */
	public int indexOf(Order order) {
		return listOrder.indexOf(order);
	}

	/**
	 * add uma ordem na lista
	 * 
	 * @param ordem
	 */
	public void onAdd(Order order) {
		listOrder.add(order);
		fireTableRowsInserted(indexOf(order), indexOf(order));
	}

	/**
	 * add uma lista de ordens
	 * 
	 * @param orderIn
	 */
	public void onAddAll(List<Order> orderIn) {
		listOrder.addAll(orderIn);
		fireTableDataChanged();
	}

	/**
	 * remove um registo da lista, atraves de um indice
	 * 
	 * @param rowIndex
	 */
	public void onRemove(int rowIndex) {
		listOrder.remove(rowIndex);
		fireTableRowsDeleted(rowIndex, rowIndex);
	}

	/**
	 * remove um registo da lista, atraves do objeto
	 * 
	 * @param order
	 */
	public void onRemove(Order order) {
		listOrder.remove(order);
		fireTableRowsDeleted(indexOf(order), indexOf(order));
	}

	/**
	 * remove todos registros da lista
	 */
	public void onRemoveAll() {
		listOrder.clear();
		fireTableDataChanged();
	}

}
